using UnityEngine;



[RequireComponent(typeof(MovementComponent))]
public class AutoTurnComponent : MonoBehaviour
{
    [Header("Turn Settings")]
    [SerializeField] private float turnSmoothTime = 0.1f;
    [SerializeField] private float turnSpeedThreshold = 0.1f;

    private MovementComponent _movement;
    private float _turnSmoothVelocity;

    void Awake()
    {
        _movement = GetComponent<MovementComponent>();
    }

    void Update()
    {
        Vector3 moveDirection = _movement.MoveDirection;

        // 仅在有有效输入时转向
        if (moveDirection.magnitude >= turnSpeedThreshold)
        {
            float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
            float smoothedAngle = Mathf.SmoothDampAngle(
                transform.eulerAngles.y,
                targetAngle,
                ref _turnSmoothVelocity,
                turnSmoothTime
            );

            transform.rotation = Quaternion.Euler(0f, smoothedAngle, 0f);
        }
    }
}